package core;

import org.kxml2.kdom.*;

public class Subject {

	private Discipline m_discipline;
	private String m_teacher;
	private String m_comment;
	private String m_room;

	public Subject(Discipline discipline, String room, String teacher,  String comments){
		m_discipline = discipline;
		m_teacher = teacher;
		m_comment = comments;
		m_room = room;
	}
	
	public Discipline getDiscipline(){
		return m_discipline;
	}
	
	public String getRoom(){
		return m_room;
	}
	
	public String getTeacher(){
		return m_teacher;
	}
	
	public String getComment(){
		return m_comment;
	}

	public void toXml(Node node){
		Element subjNode = node.createElement(null, "Subject");
		subjNode.setAttribute(null, "room", m_room);
		subjNode.setAttribute(null, "teacher", m_teacher);
		m_discipline.toXml(subjNode);
		Element commentElem = subjNode.createElement(null, "Comment");
		commentElem.addChild(Node.TEXT, m_comment);
		subjNode.addChild(Node.ELEMENT, commentElem);
		node.addChild(Node.ELEMENT, subjNode );        
	}
	
	public static Subject fromXml(Element elem) throws Throwable{
		try
	    {
			Element rootElem = elem; 
			if (!rootElem.getName().equals("Subject")){
				rootElem = elem.getElement(null, "Subject");
			}
			
			String room = rootElem.getAttributeValue(null, "room");
			String teacher = rootElem.getAttributeValue(null, "teacher");
			Discipline discipline = Discipline.fromXml(rootElem);
			String comments = rootElem.getElement(null, "Comment").getText(0);
			return new Subject(discipline, room, teacher,  comments);
	    }
		catch (Exception e) {
			throw e;
		}
	}
}
